﻿#region GPLv2 license
/*
Copyright (C) 2009-2010 Jamm Daemon project <http://code.google.com/p/jammd/>

Jamm is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jamm.SharedLib.Misc
{
    static class JByte
    {
        public static void Resize(ref byte[] bytes, UInt64 size)
        {
            var tempReDim = new byte[size];
            if (bytes != null)
                Array.Copy(bytes, tempReDim, System.Math.Min(bytes.Length, tempReDim.Length));
            bytes = tempReDim;
        }

        public static void Resize(ref byte[] bytes, int size)
        {
            var tempReDim = new byte[size];

            if (bytes != null)
                Array.Copy(bytes, tempReDim, System.Math.Min(bytes.Length, tempReDim.Length));
            bytes = tempReDim;
        }

        public static byte[] Copy(ref byte[] src_bytes, int src_offset)
        {
            if (src_bytes.Length - src_offset < 0)
                throw new Exception("Cannot create a new array of bytes with size < 0");

            var tempReDim = new byte[src_bytes.Length - src_offset];

            Array.Copy(src_bytes, src_offset, tempReDim, 0, src_bytes.Length - src_offset); // + 1); // Is the +1 part correct? Can anyone check pls? :)

            return tempReDim;
        }

        public static byte[] Copy(ref byte[] src_bytes, int src_offset, int length)
        {
            if (src_bytes.Length - src_offset < 0)
                throw new Exception("Cannot create a new array of bytes with size < 0");

            var tempReDim = new byte[length];

            if (length > src_bytes.Length - src_offset)
            { 
                length = src_bytes.Length - src_offset;
            }

            Array.Copy(src_bytes, src_offset, tempReDim, 0, length);

            return tempReDim;
        }

        public static byte[] ConvertASCII(string myData)
        {
            var encoding = new ASCIIEncoding();

            return encoding.GetBytes(myData);
        }

        public static byte[] ConvertUTF8(string myData)
        {
            var encoding = new UTF8Encoding();

            return encoding.GetBytes(myData);
        }
        public static byte[] ConvertUnicode(string myData)
        {
            var encoding = new UnicodeEncoding();

            return encoding.GetBytes(myData);
        }
    }
}
